#include <slothttpd/fcgi/application.hpp>
#include <slothttpd/server.hpp>
#include <slothttpd/logger.hpp>

#include <stdexcept>

#include <boost/exception/all.hpp>

namespace slothttpd {
namespace fcgi {

multiplexer::request_controller application::begin_request(
        receive_buffer_list *stdout,
        receive_buffer_list *stderr, 
        multiplexer::request_id::get_write_buffer_type get_buffers,
        multiplexer::request_id::handler_type handler)
{
    if (not_full_connections.empty()) {
        multiplexer_ptr m = new multiplexer(this);
        
        return m->begin_request(stdout, stderr, get_buffers, handler);
    } else {
        multiplexer &m = not_full_connections.front();
        
        return m.begin_request(stdout, stderr, get_buffers, handler);
    }
}

void application::close()
{
    for (auto &m : full_connections) {
        m.close();
    }
    
    for (auto &m : not_full_connections) {
        m.close();
    }
}

}
}
